setInterval( init, 4000 );

var img_list = "";
var news_list = "";
var template_var = "";
var template_type = "";
var vid_list = "";
var update_img = "0";
var update_txt = "0";
var update_txt_fixe = "0";
var update_vid = "0";
var delais;

var config = {
	imagesUrl : "img_list.json",
	videosUrl : "vid_liste.json",
	fixedTextUrl : "text_fixe.json",
	templateUrl : "template.json",
	textUrl : "text_list.json"
};

function ajax_videos(){
   	$(document).ready(function(){
    	var player = projekktor('#first_div', {
    		debug: false,
    		poster: 'intro.png',
    		useYTIframeAPI: false,
    		autoplay: true,
    		loop:true,
    		enableTestcard: false,
        	plugin_display: {logoImage: "yourlogo.png",logoDelay: 2},
        	controls: true,
		});
		player.setFile(config.videosUrl,'text/json');
    });
}

function ajax_images(){
    $.ajax({
    	method: "POST",
    	url: config.imagesUrl,
    	dataType: "json",
    	success : function(result){
			img_list = "";
			for (i=0;i<result.images.length;i++){
				img_list += "<img src='./"+result.images[i]+"' />";
			}
			var div_img_obj = document.getElementById("second_div");
			div_img_obj.innerHTML = img_list;

			document.getElementById('second_div').innerHTML = img_list	
				$(document).ready(function() {
				$('#second_div').cycle({fx: 'fade',timeout: '3000',fit :1,width:"100%",height:"100%"});
				midify_template();
			});		
		
    	}
    });
  }

function ajax_text(){	
    $.ajax({
    	method: "POST",
    	url: config.textUrl,
    	dataType: "json",
    	success : function(result){
    		news = result.news
			for (j=0;j<news.length;j++){
				$("#third_div").append("<marquee direction=\"up\" scrollamount=\"2\" >"+news[j]+"</marquee>");
			}
    	}
    });
}

function ajax_text_fixe(){
    $.ajax({
    	method: "POST",
    	url: config.fixedTextUrl,
    	dataType: "json",
    	success : function(result){
 			$('#fourth_div').html(result.news[0]);    		
    	}
    });
}

function init(){	
    $.ajax({
    	method: "POST",
    	url: config.templateUrl,
    	dataType: "json",
    	success : function(result){
			var template_var = result.type;
			var img_update = result.update_img;
			var txt_update = result.update_txt;
			var vid_update = result.update_vid;
			var update_text_fixe = result.text_fixe;
			delais_obj = result.delais;
			
			if(delais != delais_obj){
				delais = delais_obj;
			}
			
			if(template_type != template_var){
				template_type = template_var;
				midify_template();
			}
			
			if(update_img != img_update){
				update_img = img_update;
				ajax_images();
			}
			
			if(update_txt != txt_update){
				update_txt = txt_update;
				ajax_text();
			}
			
			if(update_vid != vid_update){
				update_vid = vid_update;
				ajax_videos();
			}
			
			if(update_txt_fixe != update_text_fixe){
				update_txt_fixe = update_text_fixe;
				ajax_text_fixe();
			}
        }
    });
}

function midify_template(){
	if(template_type == 1){
		$(document).ready(function() {
			$("#first_div").hide(1000);
			$("#second_div").show(1000);
			$("#third_div").hide(1000);
			$("#fourth_div").hide(1000);
			$("#second_div").animate({height:"100%",width:"100%",top:"0%",right:"0%"},1500,"swing");
		})
	}
	
	if(template_type == 2){
		$(document).ready(function() {					 					
			$("#second_div").animate({height:"80%",width:"100%",top:"0%",right:"0%"},1500,"swing");
			$("#third_div").animate({height:"20%",width:"100%",top:"80%",left:"0%"},1500,"swing");
	
			$("#first_div").hide(1000);
			$("#second_div").show(1000);
			$("#third_div").show(1000);
			$("#fourth_div").hide(1000);
		})
	}
	
	if(template_type == 3){
		$(document).ready(function() {
			$("#first_div").animate({width:"50%",height:"80%"},1500,"linear");
			$("#second_div").animate({width:"50%",height:"80%",top:"0%",right:"0%"},1500,"swing");
			$("#third_div").animate({width:"100%",height:"20%",top:"80%",left:"0%"},1500,"swing");
			
			$("#first_div").show(1000);
			$("#second_div").show(1000);
			$("#third_div").show(1000);
			$("#fourth_div").hide(1000);					
		})	
	}
	
	if(template_type == 4){
		$(document).ready(function() {
		    $("#third_div").animate({width:"50%",height:"50%",top:"50%",left:"0%"},1500,"swing");
		    $("#second_div").animate({width:"50%",height:"50%",top:"0%",right:"0%"},500,"swing");
			$("#first_div").animate({height:"50%",width:"50%",top:"0%",right:"100%"},1500,"linear");
			$("#fourth_div").animate({height:"50%",width:"50%",top:"50%",right:"0%"},1500,"linear");
	
			$("marquee").attr("direction","up");
			$("#first_div").show(1000);
			$("#second_div").show(1000);
			$("#fourth_div").show(1000);
			$("#third_div").show(1000);
		})
	}
}
